/*****************************************************************************//** 
 *  \brief     
 *  \author    
 *  \version   
 *  \date      
 *  \copyright
 *	\notes
******************************************************************************/
/*** Includes *****************************************************************/
#include "NArchive.h"
#include "NSerializable.h"
#include "NFile.h"
#include <sstream>
/*** Using ********************************************************************/
using namespace Core;
/*** Macros *******************************************************************/
/*** Constants ****************************************************************/
/*** Declarations *************************************************************/
/*** Variables ****************************************************************/
/*** Interface ****************************************************************/
NArchive::NArchive(NFile& file)
	:mFile(&file)
{
}

NArchive::~NArchive(void)
{
}

void NArchive::Open()
{
	std::stringstream sStream;

	switch(mFile->GetMode())
	{
	case Mode_Read:
		mVersion = mFile->Read<int>();
		break;
	case Mode_Write:
		sStream << sVersion;
		mFile->Write(sStream.str().c_str(),sStream.str().length());
		break;
	case Mode_Append:
		break;
	default:
		break;
	}
}

void NArchive::Close()
{
	mFile->Close();
}

unsigned int NArchive::GetVersion()
{
	return mVersion;
}

void NArchive::operator<< (bool& value)
{
	std::stringstream sStream;

	sStream << value;

	mFile->Write(sStream.str().c_str(), sStream.str().length());
}

void NArchive::operator<< (int& value)
{
	std::stringstream sStream;

	sStream << value;

	mFile->Write(sStream.str().c_str(), sStream.str().length());
}

void NArchive::operator<< (unsigned int& value)
{
	std::stringstream sStream;

	sStream << value;

	mFile->Write(sStream.str().c_str(), sStream.str().length());
}

void NArchive::operator<< (float& value)
{
	std::stringstream sStream;

	sStream << value;

	mFile->Write(sStream.str().c_str(), sStream.str().length());
}

void NArchive::operator<< (double& value)
{
	std::stringstream sStream;

	sStream << value;

	mFile->Write(sStream.str().c_str(), sStream.str().length());
}

void NArchive::operator<< (const char* value)
{
	int size = strlen(value);

	mFile->Write(value, size);
}

void NArchive::operator<< (NSerializable* obj)
{
	if(obj != NULL)
	{
		obj->Serialize(*this);
	}
}

void NArchive::operator>> (bool& )
{
	mFile->Read<bool>();
}

void NArchive::operator >> (int& member)
{
	int value = mFile->Read<int>();
	member = value;
}

void NArchive::operator>> (unsigned int& member)
{
	unsigned int value = mFile->Read<unsigned int>();
	member = value;
}


void NArchive::operator>> (float& member)
{
	float value = mFile->Read<float>();
	member = value;
}

void NArchive::operator>> (double& member)
{
	double value = mFile->Read<double>();
	member = value;
}

void NArchive::operator>> (const char* member)
{
	const char* value = mFile->Read<const char*>();
	member = value;
}

void NArchive::operator >> (NSerializable* obj)
{
	if(obj != NULL)
	{
		obj->Deserialize(*this);
	}
}

